/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * OutputPanel.java
 *
 * Created on Apr 19, 2011, 8:26:32 PM
 */
package com.bff.mpd.musicjukebox.ui.settings;

import com.bff.bjj.ui.component.BJJPanel;
import com.bff.bjj.utils.Resources;
import com.bff.mpd.musicjukebox.lib.JukeboxProperties;
import com.bff.mpd.musicjukebox.lib.actions.OutputAction;
import java.awt.Component;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.Action;
import javax.swing.DefaultListCellRenderer;
import javax.swing.DefaultListModel;
import javax.swing.JList;
import org.bff.javampd.MPDOutput;
import org.bff.javampd.events.OutputChangeEvent;
import org.bff.javampd.events.OutputChangeListener;
import org.bff.javampd.exception.MPDConnectionException;
import org.bff.javampd.exception.MPDResponseException;

/**
 *
 * @author Bill
 */
public class OutputPanel extends BJJPanel implements OutputChangeListener {

    private DefaultListModel outputModel;
    private JukeboxProperties jp;
    private Action outputAction;

    /** Creates new form OutputPanel */
    public OutputPanel() {
        jp = JukeboxProperties.getInstance();
        loadOutputs();
        makeActions();
        initComponents();
        addListeners();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        outputList = new javax.swing.JList();
        outputList.setCellRenderer(new OutputRenderer());
        jPanel1 = new BJJPanel();
        btnEnable = new javax.swing.JButton();

        setLayout(new java.awt.BorderLayout());

        outputList.setModel(outputModel);
        outputList.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                outputListValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(outputList);

        add(jScrollPane1, java.awt.BorderLayout.CENTER);

        btnEnable.setAction(outputAction);
        btnEnable.setEnabled(false);
        jPanel1.add(btnEnable);

        add(jPanel1, java.awt.BorderLayout.SOUTH);
    }// </editor-fold>//GEN-END:initComponents

    private void outputListValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_outputListValueChanged
        getOutputAction().setOutput(getSelectedOutput());
    }//GEN-LAST:event_outputListValueChanged
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnEnable;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JList outputList;
    // End of variables declaration//GEN-END:variables

    private void loadOutputs() {
        if (outputModel == null) {
            outputModel = new DefaultListModel();
        } else {
            outputModel.clear();
        }

        try {
            for (MPDOutput output : jp.getMPD().getMPDAdmin().getOutputs()) {
                outputModel.addElement(output);
            }
        } catch (MPDConnectionException ex) {
            Logger.getLogger(OutputPanel.class.getName()).log(Level.SEVERE, null, ex);
        } catch (MPDResponseException ex) {
            Logger.getLogger(OutputPanel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void makeActions() {
        outputAction = new OutputAction(jp.getAdmin(), null);
    }

    private OutputAction getOutputAction() {
        return (OutputAction) outputAction;
    }

    private MPDOutput getSelectedOutput() {
        return (MPDOutput) outputList.getSelectedValue();
    }

    @Override
    public void outputChanged(OutputChangeEvent event) {
        loadOutputs();
    }

    private void addListeners() {
        jp.getMPDMonitor().addOutputChangeListener(this);
        jp.getAdmin().addOutputChangeListener(this);
    }

    private class OutputRenderer extends DefaultListCellRenderer {

        @Override
        public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
            if (value instanceof MPDOutput) {
                MPDOutput output = (MPDOutput) value;
                if (output.isEnabled()) {
                    setIcon(Resources.getIcon(Resources.ICON_KEY.ICON_CHECKED));
                } else {
                    setIcon(Resources.getIcon(Resources.ICON_KEY.ICON_X));
                }
                setText(output.getName());
                return this;
            } else {
                return super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
            }

        }
    }
}
